﻿using Core.Framework.Util;
using SDK.ApiService.TrackNode.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace SDK.ApiService.TrackNode
{
    /// <summary>
    /// 物流查询服务
    /// </summary>
    public class TrackNodeService
    {
        public const string Service = "http://wuliu.market.alicloudapi.com/kdi";

        public string No { get; set; }
        public string Type { get; set; }
        public string Authorization { get; set; }

        public TrackNodeService(TrackNodeParameter parameter)
        {
            this.Authorization = parameter.Authorization;
            this.No = parameter.No;
            this.Type = parameter.Type;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public TrackNodeResult Select()
        {
            HttpWebRequest httpWebRequest = WebRequest.Create($"{Service}?no={this.No}{(string.IsNullOrWhiteSpace(this.Type) ? "" : $"&type={this.Type}")}") as HttpWebRequest;

            httpWebRequest.Method = "GET";

            httpWebRequest.Headers.Add("Authorization", $"APPCODE {this.Authorization}");

            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            WebResponse webResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            Stream dataStream = webResponse.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
            string returnStr = reader.ReadToEnd();
            reader.Close();
            webResponse.Close();

            return returnStr.TryToEntity<TrackNodeResult>();

        }


    }
}
